package com.cqeec.waimai4.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cqeec.waimai4.common.R;
import com.cqeec.waimai4.entity.Grade;
import com.cqeec.waimai4.service.GradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("grade")
public class GradeController {
    @Autowired
    GradeService gradeService;
    // 1.查询java成绩>75且vue>90的数据按照python成绩降序
    @PostMapping("getList")
    public R<List<Grade>> getList(){
        // 1、实例化条件构造器
        LambdaQueryWrapper<Grade> wrapper=new LambdaQueryWrapper<>();
        // 2、添加条件
        wrapper.gt(Grade::getJava,70)
                .gt(Grade::getVue,90)
                .orderByDesc(Grade::getPython);
        // 3、调用查询方法
        List<Grade> list=gradeService.list(wrapper);
        return R.success(list);
    }



    // 2.查询python>80或者java>80的数据按照id降序
    @PostMapping("getList2")
    public R<List<Grade>> getList2(){
        // 1、实例化条件构造器
        LambdaQueryWrapper<Grade> wrapper=new LambdaQueryWrapper<>();
        // 2、添加条件
        wrapper.gt(Grade::getPython,80).or()
                .gt(Grade::getJava,80).orderByDesc(Grade::getId);
        // 3、调用查询方法
        List<Grade> list = gradeService.list(wrapper);
        return R.success(list);
    }



    // RequestBody json类型素数据转为实体类对象
    @PostMapping("addGrade")
    public R<String> addGrade(@RequestBody Grade grade){
        boolean save = gradeService.save(grade);
        if (save){
            return R.success("添加成功");
        }
        return R.error("添加失败");
    }



    // 修改
    // 将id为5的数据修改为java100 python95
    @PostMapping("updateGrade")
    public R<String> updateGrade(){
        // update grade set java=100,python=95 where id=5
        LambdaUpdateWrapper<Grade> wrapper=new LambdaUpdateWrapper<>();
        wrapper.set(Grade::getJava,100)
                .set(Grade::getPython,95)
                .eq(Grade::getId,5);
        boolean update = gradeService.update(wrapper);
        if (update) {
            return R.success("修改成功");
        }
        return R.error("修改失败");
    }


    // 删除
    @PostMapping("deleteGrade")
    public R<String> deleteGrade(@RequestBody Grade grade) {
        // delete from grade where id=5
        LambdaQueryWrapper<Grade> wrapper  = new LambdaQueryWrapper<>();
        wrapper.eq(Grade::getId,5);
        boolean remove = gradeService.remove(wrapper);
        if (remove){
            return R.success("删除成功");
        }
        return R.error("删除失败");
    }



    // 分页查询
    @GetMapping("/getPage/{current}/{size}")
    public R<Page<Grade>> getPage(@PathVariable int current,
                                  @PathVariable int size){
        // 构建分页参数
        Page<Grade> page = new Page<>(current,size);
        // 添加查询条件
        LambdaQueryWrapper<Grade> wrapper=new LambdaQueryWrapper<>();
        wrapper.orderByDesc(Grade::getId);
        // 调用方法
        Page<Grade> page1 = gradeService.page(page, wrapper);
        return R.success(page1);
    }
}